package impl;

import bbcom.ImplementationType;
import bbcom.interfaces.IImplementationParser;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thanh-Trung Pham.
 */
public class ImplementationParser implements IImplementationParser {

    @Override
    public String getImplementationClass(ImplementationType implementation) {
        if (implementation == null) {
            return null;
        }

        XmlObject[] oImplementationJavas = implementation.selectPath("$this/@impl.java");

        if (oImplementationJavas == null || oImplementationJavas.length == 0) {
            return null;
        }

        return ((XmlAnyTypeImpl) oImplementationJavas[0]).getStringValue();

    }

    @Override
    public List<String> getImplementationDependencies(ImplementationType implementation) {
        List<String> oReturnDependencies = new ArrayList<>();

        if (implementation == null) {
            return oReturnDependencies;
        }

        XmlObject[] oDependencies = implementation.selectPath("$this/Impl.Dependencies/Dependency");
        if(oDependencies == null || oDependencies.length ==0) {
            return oReturnDependencies;
        }

        for(XmlObject oDependency: oDependencies){
            String strDependency = ((XmlAnyTypeImpl) oDependency).getStringValue();
            if(!oReturnDependencies.contains(strDependency)){
                oReturnDependencies.add(strDependency);
            }
        }

        return oReturnDependencies;
    }

}
